package com.android.fragmentdemo
import android.os.Bundle
import android.util.Log
import com.android.fragmentdemo.databinding.ActivityRoomDatabaseUseBinding
import com.android.fragmentdemo.roomdb.Pet
import com.android.fragmentdemo.roomdb.RoomDBUserUtil
import com.android.fragmentdemo.roomdb.User
import com.hengfoxwolf.commonbase.base.BaseActivity

class RoomDataBaseUseActivity : BaseActivity<ActivityRoomDatabaseUseBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_room_database_use

    override fun init(savedInstanceState: Bundle?) {
        val hobbies = mutableListOf("游泳", "看书", "羽毛球")
        val petList = mutableListOf(Pet("pet1091", "旺财", mutableListOf("骨头", "鱼")))

        val user0 = User("1234", "user0", 20, "user0--user0", "深圳龙华", hobbies, 0, "", petList)

        val user1 = User("67890", "user1", 30, "user1--user1", "深圳龙岗", hobbies, 0, "", petList)


        mBinding.btnInsert.setOnClickListener {
            Log.d(TAG, "btnInsert")
            RoomDBUserUtil.insertUser(user0, user1)
        }

        mBinding.btnQuery.setOnClickListener {
            Log.d(TAG, "btnQuery")
            val userList = RoomDBUserUtil.queryAll()
            Log.d(TAG, "size:${userList.size}")
            for (user in userList) {
                Log.d(TAG, "user:$user")
            }
        }

        mBinding.btnUpdate.setOnClickListener {
            val id = "67890"
            //val hobbies = mutableListOf("游泳", "看书", "编程")
            //RoomDBUserUtil.update(id, mutableListOf("游泳", "看书", "编程"))
            RoomDBUserUtil.update2(id, petList)
        }

        mBinding.btnDelete.setOnClickListener {
            RoomDBUserUtil.delete(user0)
        }
    }

    companion object {
        const val TAG = "RoomDataBaseUse"
    }
}